import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:loxen/api/api.dart';
import 'package:loxen/mode/base_mode.dart';
import 'package:loxen/mode/order_detail_mode.dart';
import 'package:loxen/view/material/material_order_receive/order_detail_list_card.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:provider/provider.dart';
import 'package:rxdart/rxdart.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/utils/app_enum_util.dart';
import 'package:loxen/utils/color.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/view/material/material_order_receive/order_detail_type_card.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/widget/my_subassembly.dart';

class _ViewModel extends BaseViewModel {
  String id;
  OrderDetailMode orderDetailMode;

  _ViewModel(this.id);

  void loadData() {
    ApiService.orderDetail(id)
        .doOnListen(() {
          mState = BaseState.LOADING;
          notifyListeners();
        })
        .flatMap((value) => compute(deCode, value.data).asStream())
        .flatMap((value) => Stream.value(value?.data))
        .listen((event) {
          if (event != null) {
            orderDetailMode = event;
            mState = BaseState.CONTENT;
          } else {
            mState = BaseState.FAIL;
          }
          notifyListeners();
        }, onError: (error, stack) {
          print(error);
          print(stack);
          mState = BaseState.FAIL;
          notifyListeners();
        });
  }

  static BaseMode<OrderDetailMode> deCode(dynamic jsonRes) {
    return BaseMode<OrderDetailMode>(jsonRes);
  }
}

class OrderDetailWidget extends StatelessWidget {
  final String id;
  final bool staff;

  const OrderDetailWidget({Key key, this.id, this.staff = false}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<_ViewModel>(
      create: (BuildContext context) {
        var model = _ViewModel(id);
        model.loadData();
        return model;
      },
      child: Scaffold(
        backgroundColor: Colours.bg_color,
        appBar: PreferredSize(
          preferredSize: Size(double.infinity, 96.h),
          child: Container(
            color: Theme.of(context).backgroundColor,
            child: SafeArea(
              child: Column(
                children: [
                  Row(
                    children: <Widget>[
                      MySubassembly.getBackIcon(context,
                          color: Theme.of(context).textTheme.subtitle2.color),
                      Expanded(
                        child: Center(
                          child: Text(
                            StringResources.ding_dan_xiang_qing,
                            style: TextStyle(
                                fontSize: 32.sp,
                                fontWeight: FontWeight.w500,
                                color: Theme.of(context).textTheme.subtitle2.color),
                          ),
                        ),
                      ),
                      MySubassembly.getSearchIcon(null, text: Constants.empty),
                    ],
                  ),
                  Divider(height: 1.h)
                ],
              ),
            ),
          ),
        ),
        body: Selector<_ViewModel, BaseState>(
            builder: (context, state, _) {
              Widget page;
              switch (state) {
                case BaseState.LOADING:
                  page = LoadingWidget();
                  break;
                case BaseState.EMPTY:
                case BaseState.FAIL:
                  page = FailWidget(
                    onClick: () => Provider.of<_ViewModel>(context, listen: false).loadData(),
                    button: '重试',
                  );
                  break;
                case BaseState.CONTENT:
                  page = SingleChildScrollView(
                    child: Container(
                      padding: EdgeInsets.fromLTRB(20.w, 16.h, 20.w, 30.h),
                      child: Column(
                        children: [
                          OrderDetailTypeCard(
                            orderDetailMode:
                                Provider.of<_ViewModel>(context, listen: false).orderDetailMode,
                            staff: staff,
                          ),
                          Container(height: 16.h,),
                          OrderDetailListCard(
                            orderDetailMode:
                                Provider.of<_ViewModel>(context, listen: false).orderDetailMode,
                            staff: staff,
                            onSuccess: () {
                              Provider.of<_ViewModel>(context, listen: false).loadData();
                            },
                          ),
                        ],
                      ),
                    ),
                  );
                  break;
              }
              return page;
            },
            selector: (_, value) => value.mState),
      ),
    );
  }
}
